package com.common;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * Created by 10742 on 2017/7/8.
 */
public class FishEPTool extends JdbcDaoSupport {

    public int insert(final int indexID, final String indexName, final String indexValue, final String tissue, final String sampleID){
        final String sql = "insert into FishResidEnvirPollu_Analysis(Index_id, Index_Name, Index_Value, Tissue, FishSample_id) values(?,?,?,?,?)";
        KeyHolder keyHolder = new GeneratedKeyHolder();
        getJdbcTemplate().update(new PreparedStatementCreator() {
            public PreparedStatement createPreparedStatement(Connection conn) throws SQLException {
                PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
                ps.setInt(1, indexID);
                ps.setString(2, indexName);
                ps.setString(3, indexValue);
                ps.setString(4, tissue);
                ps.setString(5, sampleID);
                return ps;
            }
        }, keyHolder);
        return keyHolder.getKey().intValue();
    }

    public int update(int fishEPID, int indexID, String indexName, String indexValue, String tissue){
        String sql = "update FishResidEnvirPollu_Analysis set Index_id=?,Index_Name=?,Index_Value=?,Tissue=? where ID=?";
        return getJdbcTemplate().update(sql, indexID, indexName, indexValue, tissue, fishEPID);
    }
}
